package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Result;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 员工分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @Transactional
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){

        //调用service分页查询
        Page<Emp> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Emp> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like( name!=null,Emp::getName,name);
        queryWrapper.like(gender!=null,Emp::getGender,gender);
        if(begin!=null&&end!=null){
            queryWrapper.ge(Emp::getEntrydate,begin).le(Emp::getEntrydate,end);
        }
        queryWrapper.orderByDesc(Emp::getUpdateTime);
        empService.page(pageInfo, queryWrapper);

        PageBean pageBean= new PageBean();
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setRows(pageInfo.getRecords());
        return Result.success(pageBean);
    }

    /**
     * 批量删除员工
     * @param ids
     * @return
     */
    @Transactional
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作, ids:{}",ids);
        empService.removeByIds(ids);
        return Result.success();
    }

    @Transactional
    @PostMapping
    public Result save(@RequestBody(required = false) Emp emp){
        log.info("新增员工, emp: {}",emp);
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empService.save(emp);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询员工信息, id: {}",id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @Transactional
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工信息 : {}", emp);
        empService.updateById(emp);
        return Result.success();
    }

}
